package com.hunan.api.catv.service.impl;

import com.hunan.api.catv.dao.BaseDao;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.system.SystemUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional
@Service
public abstract class CommonService<P, V, PK extends Serializable> extends ResultService implements BaseService<P, V, PK> {

    public abstract BaseDao<P, V, PK> getBaseDao();

    @Autowired
    private BaseDao<P, V, PK> baseDao;

    @Autowired
    SystemUserBiz systemUserBiz;

    public void setBaseDao(BaseDao<P, V, PK> baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public V getById(PK pk) {
        return baseDao.getById(pk);
    }


    @Override
    public V get(P p) {
        return baseDao.get(p);
    }

    @Override
    public ServiceResult add(P p) {
        int count = baseDao.add(p);
        return buildOK(count, "");
    }

    @Override
    public ServiceResult update(P p) {
        int count = baseDao.update(p);
        return buildOK(count, "");
    }

    @Override
    public List<V> list(P p) {
        return baseDao.list(p);
    }

    @Override
    public List<PK> listIds(P p) {
        return baseDao.listIds(p);
    }

    @Override
    public ServiceResult delete(PK pk) {
        return buildOK(baseDao.delete(pk), "");
    }

    @Override
    public ServiceResult batchDelete(P p) {
        return buildOK(baseDao.batchDelete(p), "");
    }

    @Override
    public ServiceResult batchAdd(List<P> list) {
        return buildOK(baseDao.batchAdd(list), "");
    }

    @Override
    public ServiceResult batchUpdate(List<P> list) {
        return buildOK(baseDao.batchUpdate(list), "");
    }

    @Override
    public Integer count(P p) {
        return baseDao.count(p);
    }

    public V simpleInfo(PK pk) {
        return baseDao.simpleInfo(pk);
    }

    @Override
    public List<V> simpleList(P p) {
        return baseDao.simpleList(p);
    }


    /**
     * 获取用户系统登录名称
     *
     * @param userId
     * @return
     */
    public String getSystemUserName(Integer userId) {
        String systemUserName = null;
        if (userId != null) {
            systemUserName = systemUserBiz.info(userId).getSysUserName();
        }
        return systemUserName;
    }
}
